package edu.hit.pact.pgse.crawler.bbs;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.hit.pact.pgse.crawler.download.FetchorFactory;
import edu.hit.pact.pgse.crawler.download.WebPageFetcher;
import edu.hit.pact.pgse.crawler.store.FileSystemWebPageStore;
import edu.hit.pact.pgse.crawler.store.WebPageStore;
import edu.hit.pact.pgse.crawler.util.Anchor;
import edu.hit.pact.pgse.util.Globals;

public class ArticleFetchorForUrl extends Thread{

	private ConcurrentLinkedQueue<Anchor> articleAnchorQueue;
	AtomicBoolean isFinished;
	
	
	public ArticleFetchorForUrl(
			ConcurrentLinkedQueue<Anchor> articleAnchorQueue,
			AtomicBoolean isFinished) {
		super();
		this.articleAnchorQueue = articleAnchorQueue;
		this.isFinished = isFinished;
	}

	@Override
	public void run(){
		while(this.articleAnchorQueue.size() > 0){
			try {
				sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.isFinished.set(true);
	}
	
	
	public static void main(String[] args) throws IOException {
		
		Log logger = LogFactory.getLog(ArticleFetchorForUrl.class);
		Globals.readLog4jProperties();
		WebPageFetcher articleFetcher = FetchorFactory.buildWebPageFetcher();
		WebPageStore articleStore = new FileSystemWebPageStore(true);
		AtomicBoolean isFinished = new AtomicBoolean(false);
		ConcurrentArrayList<ArticleUpdateInfo> updateList = new ConcurrentArrayList<ArticleUpdateInfo>();
		ConcurrentLinkedQueue<Anchor> articleAnchorQueue = new ConcurrentLinkedQueue<Anchor>();
		
//		Anchor ch = new Anchor();
		URL url = new URL("http://www.tianya.cn/publicforum/content/worldlook/1/200740.shtml");
//		ch.setText("中美断背山");
//		ch.setUrl(url);
//		articleAnchorQueue.add(ch);
//		Anchor ch2 = new Anchor();
//		URL url2 = new URL("http://www.tianya.cn/publicforum/content/worldlook/1/161233.shtml");
//		ch2.setText("谁为谁狂：小民评点环球时事专帖[最新：系列263]");
//		ch2.setUrl(url2);
//		articleAnchorQueue.add(ch2);
//		int threadNum = 4;
//		if(articleAnchorQueue.size() <= 4){
//			threadNum = articleAnchorQueue.size();
//		}
//		
//		ArticleFetchorForUrl managerThread = new ArticleFetchorForUrl(articleAnchorQueue,isFinished);
//		managerThread.start();
//		List<AbstractBBSFetchorThread> articleThreads = new ArrayList<AbstractBBSFetchorThread>();
//		for (int i = 0; i < threadNum; ++i) {
//			AbstractBBSFetchorThread articleFetchor = new BBSArticleFetchProcessor(
//					logger, articleFetcher, articleStore, articleAnchorQueue,
//					"task_s" + "_articleThread_"+i, "forSingle", isFinished,updateList,false);
//			articleThreads.add(articleFetchor);
//		}
//		
//		for (AbstractBBSFetchorThread thread : articleThreads) {
//			thread.start();
//		}
		
		ArticleUpdateInfo uinfo1 = new ArticleUpdateInfo();
		uinfo1.setHomepage(url);
		URL lastpage = new URL("http://www.tianya.cn/publicforum/content/worldlook/1/311280.shtml");
		uinfo1.setLastpage(lastpage);
		uinfo1.setPageNum(1932);
		uinfo1.setFolderName("forSingle/task_s_articleThread_0-1-中美断背山/");
		uinfo1.setPageNum(1932);
		updateList.add(uinfo1);
		
//		ArticleUpdateInfo uinfo2 = new ArticleUpdateInfo();
//		uinfo2.setHomepage(url2);
//		URL lastpage2 = new URL("http://www.tianya.cn/publicforum/content/worldlook/1/290263.shtml");
//		uinfo2.setLastpage(lastpage2);
//		uinfo2.setPageNum(1577);
//		uinfo2.setFolderName("forSingle/task_s_articleThread_1-1-谁为谁狂：小民评点环球时事专帖[最新：系列263]/");
//		uinfo2.setPageNum(1577);
//		updateList.add(uinfo2);
		
		BBSArticleUpdateFetchor updateFetchor = new BBSArticleUpdateFetchor(logger,"task_s" + "_updateFetchor",updateList);
		updateFetchor.setInterval(0);
		updateFetchor.start();
	}

}
